<template>
    <div style="width: 100%;height: 100vh;overflow:hidden ;background-color:#cccccc;">
    <div style="width: 400px;margin: 150px auto">
      <div style="color: black; padding: 30px 0; font-size: 30px;text-align: center">欢迎登陆</div>
      <el-form ref="formRef" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" type="password" v-model="form.password" style=""></el-input>
        </el-form-item>

        <el-form-item>
          <div style="display: flex">
            <el-input placeholder="请输入验证码" prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%;"></el-input>
            <ValidCode @input="createValidCode"/>
          </div>
        </el-form-item>

        <el-form-item>
          <el-radio v-model="role" :label="1">管理员</el-radio>
          <el-radio v-model="role" :label="2">普通用户</el-radio>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%;" @click="login" type="primary">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%;" @click="toRegister" type="primary">注册</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import ValidCode from '@/components/ValidCode'

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    return {
      form: {role:1},
      validCode:"",
      role:1,
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
      }
    }
  },
  methods: {
    //接收验证码组件提交的4位验证码
    createValidCode(data){
      this.validCode = data
    },
    toRegister(){
      console.log("跳转")
      this.$router.push("/register")
    },
    login() {
      //满足校验规则才传给后台数据
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          //比较验证码
          if (!this.form.validCode)
          {
            this.$message.error("请输入验证码")
            return
          }
          if (this.form.validCode.toLowerCase()!== this.validCode.toLowerCase())
          {
            this.$message.error("验证码错误")
            return;
          }


          //将登录信息传给后台
          request.post("/user/login", this.form).then(res => {
            console.log(this.form)

            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "登陆成功"
              })
              //判断权限是否选择正确
              if (res.data.role !== this.role) {
                console.log("选择：" + this.role)
                console.log(res.data)
                console.log("实际：" + res.data.role)
                this.$message.error("没有该权限")
                return
              }

              //登陆成功后将用户信息存到sessionStorage
              // console.log(res)
              sessionStorage.setItem("user",JSON.stringify(res.data))
              // console.log("sessionStorage后:"+sessionStorage.getItem("user"))
              this.$router.push("/") //登录成功后进行页面跳转 跳转到主页
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }


          })
        }
      })

    }
  }
}

</script>

<style scoped>

</style>